﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exam
{
    public partial class CMD : Form
    {
        public CMD()
        {
            InitializeComponent();
        }

        #region 整体
        private void CMD_Load(object sender, EventArgs e)
        {

            //让表显示数据
            var sqlCMD = "select * from Articles";
            var datatable = DBHelp.DataTable(sqlCMD);//调用DBHelp方法
            dataGridView1.DataSource = datatable;

            //使表内的数据只能读取不能修改
            dataGridView1.ReadOnly = true;
            //选择整行
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //去除最后一行空白行
            dataGridView1.AllowUserToAddRows = false;
        }
        #endregion

        #region 搜索
        private void font_Click(object sender, EventArgs e)
        {
            string sqlCMD = string.Format("select * from Articles where Title like '%{0}%' or Author like '%{1}%' or id like '%{2}%'", textBox1.Text, textBox1.Text, textBox1.Text);
            var datatable = DBHelp.DataTable(sqlCMD);
            dataGridView1.DataSource = datatable;
        }

        #endregion

        #region 添加
        private void add_Click(object sender, EventArgs e)
        {
            Add add = new Add();
            var res = add.ShowDialog();
            if (res == DialogResult.Yes)
            {
                string sqlCMD = "select * from Articles";
                var datatable = DBHelp.DataTable(sqlCMD);
                dataGridView1.DataSource = datatable;
            }
        }
        #endregion

        #region 更新
        private void update_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {  //取值
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                var id = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;
                Add add = new Add(title, author, createdTime, id);
                var res = add.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    string sqlCMD = "select * from Articles";
                    var datatable = DBHelp.DataTable(sqlCMD);
                    dataGridView1.DataSource = datatable;
                }
            }
            else
            {
                MessageBox.Show("未选择任何数据！", "提示");
            }
        }
        #endregion

        #region 删除      
        private void delete_Click(object sender, EventArgs e)
        {
            var Array = dataGridView1.SelectedRows[0];
            var IDS = Array.Cells["id"].Value;
            if (IDS != null)
            {
                var tips = MessageBox.Show("Are you sure?", "Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (tips.Equals(DialogResult.Yes))
                {
                    string sqlCMD = string.Format("delete from Articles where id='{0}'", IDS);
                    var EXT = DBHelp.ChangeSave(sqlCMD);
                    if (EXT > 0)
                    {
                        string sql = string.Format("select * from Articles where  Title like '%{0}%' or Author like '%{1}%' ", textBox1.Text, textBox1.Text);
                        var get = DBHelp.DataTable(sql);
                        dataGridView1.DataSource = get;
                        MessageBox.Show("删除成功", "提示");
                    }
                }
            } else
                {
                    MessageBox.Show("未选择任何数据", "提示");
                }
        }

        #endregion


    }
}
